//
//  ZYChoiceItemView.m
//  KaKaWallet
//
//  Created by ifly on 2017/11/28.
//  Copyright © 2017年 ibendi. All rights reserved.
//

#import "ZYChoiceItemView.h"

//#define kkStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
//#define kkNavBarHeight 44.0
////注意：请直接获取系统的tabbar高度，若没有用系统tabbar，建议判断屏幕高度；之前判断状态栏高度的方法不妥，如果正在通话状态栏会变高，导致判断异常，下面只是一个例子，请勿直接使用！
//#define kkTopHeight (kStatusBarHeight + kNavBarHeight)

@implementation ZYChoiceItemModel
@end


@implementation ZYChoiceItemViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        
        self.kkNamebl = [UILabel new];
        [self.contentView addSubview:self.kkNamebl];
        self.kkNamebl.textAlignment = NSTextAlignmentCenter;
        
        [self.kkNamebl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    
    return self;
}

-(void)setKkModel:(ZYChoiceItemModel *)kkModel
{
    _kkModel = kkModel;
    self.kkNamebl.text = _kkModel.kkName;
}
@end


@implementation ZYChoiceItemView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor redColor];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout layoutAttributesForElementsInRect:CGRectZero];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 3)/4, ([UIScreen mainScreen].bounds.size.width - 3)/4);
        layout.minimumInteritemSpacing = 1; //item 间距
        layout.minimumLineSpacing = 1;// 行间距
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        _kkRootView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,[[UIApplication sharedApplication] statusBarFrame].size.height+44, frame.size.width, frame.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height- 44) collectionViewLayout:layout];
        _kkRootView.dataSource = self;
        _kkRootView.delegate = self;
        [self addSubview:_kkRootView];
        _kkRootView.backgroundColor = [UIColor whiteColor];
        [_kkRootView registerClass:[ZYChoiceItemViewCell class] forCellWithReuseIdentifier:@"ZYChoiceItemViewCell"];
//        self.kkRootView.backgroundColor = HXCustomColor(244, 244, 244, 1);
    }
    return self;
}
+(ZYChoiceItemView *)shareChoiceView
{
    ZYChoiceItemView *choiceView = [[ZYChoiceItemView  alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    return choiceView;
}
-(void)show
{
//    _backView.center = CGPointMake(self.bounds.size.width/2,-self.bounds.size.height/2);
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
//        _backView.center = CGPointMake(self.bounds.size.width/2,self.bounds.size.height/2);
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    } completion:^(BOOL finished) {
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.kkDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYChoiceItemViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYChoiceItemViewCell" forIndexPath:indexPath];
    
    ZYChoiceItemModel *model = self.kkDataArray[indexPath.row];
    [cell setKkModel:model];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    KKRootCollModel *model = self.kkRootArray[indexPath.row];
//    [self loadCreateMenuClick:model];
}

-(void)setKkDataArray:(NSArray<ZYChoiceItemModel *> *)kkDataArray
{
    _kkDataArray = kkDataArray;
    
    [_kkRootView reloadData];
}





@end
